#!/usr/bin/perl -w
#-----------------------------------------------------------+
#                                                           |
# ShortFasta.pl                                             |
#                                                           |
#-----------------------------------------------------------+
#                                                           |
#  AUTHOR: James C. Estill                                  |
# CONTACT: JamesEstill_@_gmail.com                          |
# STARTED: 01/28/2007                                       |
# UPDATED: 04/23/2007                                       |
#                                                           |
# DESCRIPTION:                                              |
#  Takes an NCBI format fasta file and changes the fasta    |
#  header to just the primary id.                           |
#                                                           |
# Example Usage:                                            |
#  ShortFasta Infile.fasta Outfile.fasta                    |
#                                                           |
#-----------------------------------------------------------+


#-----------------------------+
# GET VARS FROM COMMAND LINE  |
# AND SET VAR SCOPE           |
#-----------------------------+
# INFILE AND OUTFILE SHOULD PROVIDE FULL PATH
my $usage = "Fasta2Dir.pl infile outfile \n";
my $infile = shift or die $usage;
my $outfile = shift or die $usage;
$SeqNum = 0;

#-----------------------------+
# FILE I/O                    |
#-----------------------------+
open (INFILE, "<$infile") ||
    die "Can not open infile:\n$infile\n";

open (OUTFILE,">$outfile") ||
    die "Can not open outfile:\n$outfile\n";

while (<INFILE>)
{
    # Remove newline character
    chomp;
    # If we are in the fasta header position
    # otherwise print the sequence information as it is
    if (m/\>.*/)
    {
	$SeqNum++;
	# TEMP EXIT FOR DEBUG
	if ($SeqNum > 100){exit;}

	my $HeaderLine = $_;
	my @HeaderInfo = split(/\|/, $HeaderLine);
	my $ShortName = $HeaderInfo[3];
	# Show the short name for testing 
	print $SeqNum.":\t".$ShortName."\n";
	print OUTFILE ">".$ShortName."\n";
	
    }else{
	print OUTFILE $_."\n";
    }
}

exit;

#-----------------------------------------------------------+
# HISTORY                                                   |
#-----------------------------------------------------------+
# 04/23/2007
# - Minor updates, made program ready for jperl svn
